﻿var gulp = require('gulp');
var del = require('del');
// 编译less
var less = require('gulp-less');
// 网页自动刷新
var livereload = require('gulp-livereload');
// 压缩js
var uglify = require('gulp-uglify');
// 压缩css
var uglifycss = require('gulp-uglifycss');
// 打开浏览器
// var opn = require('opn');
// web本地服务器
var webserver = require('gulp-webserver');
// 重命名
var rename = require('gulp-rename');
// 替换
var replace = require('gulp-replace');

//压缩
var zip = require('gulp-zip');

//只编译更改的文件
var watch = require('gulp-watch');

//只复制更改的文件
var changed = require('gulp-changed');

var config = require('./config.json');

// 压缩html
// var htmlmin = require('gulp-htmlmin');
var paths = {
	less:['src/css/*.less'],
	copy:['src/js/*.js','src/assets/**','src/views/**','src/css/*.css','src/*.html']
};

var time = new Date().getTime();

var path = config.name + "-" + time;

var buildPath = 'build/' + config.name; 

var lib = config.lib;


//清空先前编译的文件
gulp.task('clean:bin', function(){
	del.sync(['bin']);
});

gulp.task('clean:build', function(){
	del.sync(['build']);
});

//复制文件
gulp.task('copy:src', function(){
	gulp.src(paths.copy[0])
		.pipe(changed('bin/js'))
		.pipe(gulp.dest('bin/js'));
	gulp.src(paths.copy[1])
		.pipe(changed('bin/assets'))
		.pipe(gulp.dest('bin/assets'));
	gulp.src(paths.copy[2])
		.pipe(changed('bin/views'))
		.pipe(gulp.dest('bin/views'));
	gulp.src(paths.copy[3])
		.pipe(changed('bin/css'))
		.pipe(gulp.dest('bin/css'));
	gulp.src(paths.copy[4])
		.pipe(changed('bin'))
		.pipe(gulp.dest('bin'));
	livereload.reload();
});

//编译less
gulp.task('less', function(){
	gulp.src(paths.less[0])
		.pipe(watch(paths.less[0]))
		.pipe(less())
		.pipe(gulp.dest('bin/css'))
	livereload.reload();
});


//刷新页面
gulp.task('reload', function(){
	livereload.reload();
});

gulp.task('webserver', function(){
	gulp.src('./')
		.pipe(webserver({
			host:config.server.host,
			port:config.server.port,
			path:'/',
			livereload:true,
			directoryListing:true,
			open:"http://"+config.server.host+":"+config.server.port+"/bin/"
		}));
});

//压缩文件
gulp.task('minifycss', function(){
	gulp.src('bin/css/*.css')
		.pipe(uglifycss())
		.pipe(rename({extname:'.min.css'}))
		.pipe(gulp.dest(buildPath+'/css'));
});

gulp.task('minifyjs', function(){
	gulp.src('bin/js/*.js')
		.pipe(uglify())
		.pipe(rename({extname:'.min.js'}))
		.pipe(gulp.dest(buildPath+'/js'));
});

//复制所有第三方库，请在config.json中描述自己用到的第三方库
gulp.task('copy:lib', function(){
	for(var v in lib) {
		var item = lib[v];
		if(item instanceof Object) {
			for(var value2 in item) {
				gulp.src(item[value2])
					.pipe(gulp.dest('bin/lib/'+v+'/'+value2));
			}
		}
		if(typeof item === "string") {
			var itemPath = 'bin/lib/'+v;
			gulp.src(item)
				.pipe(gulp.dest(itemPath));
		}
	}
});


gulp.task('copy:bin', function(){
	gulp.src('bin/*.html')
		.pipe(replace('.js','.min.js'))
		.pipe(replace('.css','.min.css'))
		.pipe(gulp.dest(buildPath));
	gulp.src('bin/assets/**')
		.pipe(gulp.dest(buildPath+'/assets'));
	gulp.src('bin/views/**')
		.pipe(gulp.dest(buildPath+'/views'));
	gulp.src('bin/lib/**')
		.pipe(gulp.dest(buildPath+'/lib'));
});

//监控文件
gulp.task('watch', function(){
	livereload.listen();
	gulp.watch(['src/assets/**',
		'src/**/*.css',
		'src/**/*.less',
		'src/**/*.js',
		'src/**/*.html'],
		['less','copy:src']);
});


//压缩文件
gulp.task('zip', function(){
	gulp.src(config.zip)
		.pipe(zip(time+'.zip'))
		.pipe(gulp.dest('build'));
});


gulp.task('build', ['clean:build','minifycss','minifyjs','copy:bin']);

gulp.task('default', ['clean:bin','copy:src','copy:lib','less','watch','webserver']);
